package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Database {
	private Connection conn = null;
	private static Database myDB;
	
	public static Database getInstance(){
		if(myDB != null)
			myDB = new Database();
		return myDB;
	}
	
	public Connection getConnection() throws SQLException {
		try {
			if (conn.isClosed()) {
				checkDriver();
				conn = DriverManager.getConnection(util.Constants.dbUrl, 
						util.Constants.dbUserName,
						util.Constants.dbPassword);
			}
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return conn;
	}

	private void checkDriver() throws ClassNotFoundException {
		Class.forName(util.Constants.dbDriverPath);
	}

	public void closeConnection() throws SQLException {
		try {
			if (conn != null && !conn.isClosed())
				conn.isClosed();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public void rollBackConnection() throws SQLException {
		try {
			if (conn != null && conn.isClosed())
				conn.rollback();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}
